// Copyright [2020] <unknown>
#include <bits/stdc++.h>
using namespace std;
#define ONLINE_JUDGE
#ifndef ONLINE_JUDGE
#define dbg(x...) { cerr << "\033[32;1m" << #x << " -> "; err(x); }
void err() { cerr << "\033[39;0m" << endl; }
template<typename T, typename... A>
void err(T a, A... x) { cerr << a << ' '; err(x...); }
#else
#define dbg(...)
#endif

#define sz(x) ((int)x.size())

typedef long long LL;

const int N = 5e4+50;

pair<int, int> dps[N];
int dpf[N];

void upt(pair<int, int> &p, int x) {
    if (x > p.first) {
        p.second = p.first;
        p.first = x;
    }
    else if (x > p.second) {
        p.second = x;
    }
}

int d[N];

int n;

vector<int> G[N];

void dfs(int u, int fa, int dep) {
    // dbg(u, fa, dep);
    d[u] = dep;
    dps[u] = make_pair(0, 0);
    for (int v : G[u]) {
        if (v == fa)
            continue;
        dfs(v, u, dep+1);
        upt(dps[u], dps[v].first + 1);
    }
    dbg(u, fa, dps[u].first, dps[u].second);
}

void dfs2(int u, int fa, int mx) {
    dbg(u, fa, mx);
    dpf[u] = mx+1;
    for (int v : G[u]) {
        if (v == fa)
            continue;
        int mmx = dps[u].first;
        if (mmx == dps[v].first+1)
            mmx = dps[u].second;
        // dbg(u, mmx);
        mmx = max(mmx, dpf[u]);
        dfs2(v, u, mmx);
    }
}

int a[N];

int main(int argc, char const *argv[]) {
    // code
    scanf("%d", &n);
    for (int i=1; i<=n; ++i)
        scanf("%d", &a[i]);
    for (int i=1; i<n; ++i) {
        int u, v;
        scanf("%d%d", &u, &v);
        G[u].push_back(v);
        G[v].push_back(u);
    }

    dfs(1, 1, 0);
    dfs2(1, 1, -1);

    LL ans = 0;
    for (int i=1; i<=n; ++i) {
        ans = max(ans, 1LL * a[i] * max(dps[i].first, dpf[i]));
        // ans *= a[i];
        dbg(i, a[i], dps[i].first, dps[i].second, dpf[i]);
    }
    printf("%lld\n", ans);
    return 0;
}
